'''
Created on Sep 20, 2011

@author: oabalbin
'''
import os
import subprocess
import lxml.etree as etree   
from optparse import OptionParser

def exeriment_folder_structure(base_folder,files_list,sp_rexp,base_config_file,base_analysis_file,set_of_parameters):
    """
    creates an output folder. Name folder is the date it was created
    returns the full path to the folder and the folder name only: outfolder, t.strftime
    """
    
    new_args=[str(set_of_parameters[0]),str(set_of_parameters[1])]
    sample_name='MQ%sQ%s'%(new_args[0],new_args[1])
    sample_folder_name=os.path.join(base_folder,sample_name)
    if not os.path.isdir(sample_folder_name):
        os.mkdir(sample_folder_name)
        sample_folder = os.path.join(sample_folder_name,'sample')
        os.mkdir(sample_folder)
        os.mkdir(os.path.join(sample_folder_name,'lane'))
        os.mkdir(os.path.join(sample_folder_name,'analysis'))
        sample_subfolder_name = os.path.join(sample_folder,sample_name)
        os.mkdir(sample_subfolder_name)
        
        # create new config files
        config_file=os.path.join(sample_folder_name,'config_'+sample_name+'.xml')
        cf = open(config_file,'w')
        ncf = config_files(base_config_file,sample_folder_name,new_args) # etree object
        print >> cf, etree.tostring(ncf, pretty_print = True)
        # create a new analysis file
        args_list=[sample_name,sample_name,'mctp_'+sample_name+'_1']
        analysis_file=os.path.join(sample_folder_name,'analysis_'+sample_name+'.xml')
        sf=open(analysis_file,'w')
        nsf=analysis_files(base_analysis_file,args_list)
        print >> sf, etree.tostring(nsf, pretty_print = True)
        cf.close()
        sf.close()
        
        for f in files_list:
            f1 = f.split('/')[-1]
            f2=os.path.join(sample_subfolder_name,f1.replace(sp_rexp,sample_name+'_'))
            #print f2
            subprocess.call(['ln','-s',f,f2])
        
        py_folder='/home2/oabalbin/workspace/debug/exome/trunk/exome/snps_callers/'
        mypython = '/nobackup/med-mctp/sw/dev/python/epd/epd-7.0-2-rh5-x86_64/bin/python'
        gatk_call = [mypython,os.path.join(py_folder,'snps_byGatk_bySingleSample.py'),
                     '-r',config_file,'-a',analysis_file,
                     '--paired_samples','--cluster','--do_indel'
                     ]
        #print gatk_call
        gatk_log = open(os.path.join(sample_folder_name,'gatk_'+sample_name+'.log'),'w')
        subprocess.call(gatk_call,stdout=gatk_log)
        
        sam_vars_call = [mypython,os.path.join(py_folder,'snps_bySamAndVarScan_bySingleSample.py'),
                     '-r',config_file,'-a',analysis_file,
                     '--paired_samples','--cluster'
                     ]
        #print sam_vars_call
        sam_vars_log = open(os.path.join(sample_folder_name,'sam_vars_'+sample_name+'.log'),'w')
        subprocess.call(sam_vars_call,stdout=sam_vars_log)
 
        

def config_files(file,output_dir,new_args):
    '''
    Read and modify (chambonamente) the config file
    '''
    MQ,Q=new_args[0],new_args[1]

    x= etree.parse(file)
    root = x.getroot()
    o = x.find("output_dir")
    o.text = output_dir
    sam = x.find("sam")
    sam[0].text='%s'%(Q)
    sam[1].text='%s'%(MQ)
    gatk = x.find("gatk")
    gatk[11].text='%s'%(Q)
    gatk[12].text='%s'%(MQ)
    
    vars = x.find("varscan")
    vars[2].text='%s'%(Q)
    
    return x

def analysis_files(file,args_list):
    analysis_name, sample_name, lane_name=args_list[0],args_list[1],args_list[2]
    x= etree.parse(file)
    r = x.getroot()
    r.set("name",analysis_name)
    s=r.find("sample")
    s.set("name",sample_name)
    l=s.find("lane")
    l.set("name",lane_name)
    
    return x


def main_experiment(base_folder,files_list,sample_name_rexp,
                    config_file,analysis_file):
    '''
    '''
    mapping_qual_vector=[13,20,30,40]
    bases_qual_vector=[13, 20,30,40]
    
    for mq in mapping_qual_vector:
        for bq in bases_qual_vector:
            params_set=[mq,bq]
            exeriment_folder_structure(base_folder,files_list,sample_name_rexp,config_file,analysis_file,params_set)
    

if __name__ == '__main__':
    
    optionparser = OptionParser("usage: %prog [options] ")
    optionparser.add_option("-r", "--config_file", dest="config_file",
                            help="file with run configuration")
    optionparser.add_option("-a", "--analysis_file", dest="analysis_file",
                            help="file with experiment configuration") 
    optionparser.add_option("--paired_samples", dest="paired_samples", action="store_true", default=False,
                            help="paired samples snv calling") 
    optionparser.add_option("--multi_samples", dest="multi_samples", action="store_true", default=False,
                            help="multi-sample snv calling")
    optionparser.add_option("--use_recal_files", dest="use_recal_files", action="store_true", default=False,
                            help="use recal files to call the snvs variants")
    optionparser.add_option("-b","--base_folder", dest="base_folder", 
                            help="base folder for analysis")
    
      
    optionparser.add_option("--local", dest="local", action="store_true", default=False)
    optionparser.add_option("--cluster", dest="cluster", action="store_true", default=False)
    optionparser.add_option("--local_cluster", dest="local_cluster", action="store_true", default=False)
    optionparser.add_option("-p", "--processes", type=int, dest="num_processors", default=1)

    (options, args) = optionparser.parse_args()    

    
    sample_name_rexp='NA12878_'
    this_folder='/nobackup/med-mctp/projects/exome/NA12878/sample/NA12878/'#'/exds/users/oabalbin/projects/exomes/NA12878/sample/NA12878/
    files_list=[os.path.join(this_folder,'NA12878_merged.quick.smdup.bam'),
                os.path.join(this_folder,'NA12878_merged.quick.smdup.bai'),
                os.path.join(this_folder,'NA12878_merged.recal.smdup.bam'),
                os.path.join(this_folder,'NA12878_merged.recal.smdup.bai')]
    
    #config_file ='/exds/users/oabalbin/projects/exomes/NA12878/config_MQ13Q13.xml'
    #analysis_file='/exds/users/oabalbin/projects/exomes/NA12878/MQ13Q13_analysis.xml'
    main_experiment(options.base_folder,
                               files_list,sample_name_rexp,
                               options.config_file,options.analysis_file)

